home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-27 | 4.4 KB | 175 lines | [TEXT/CWIE] |
- /* SK8 © 1997 Apple Computer, Inc.
- This code is protected under the current SK8 License
- See http://sk8.research.apple.com/ for more information
- Apple Research Laboratories
- */
-
-
- import java.awt.*;
-
- class gradient extends renderer {
- //CONSTANTS FOR DIRECTION OF GRADIENT.
- public static int horizontal = 0;
- public static int vertical = 1;
- public static int oval = 2;
- public static int rect = 3;
-
- //PROPERTIES AND THEIR GETTERS AND SETTERS.
- int startredVar;
- int startgreenVar;
- int startblueVar;
- int endredVar;
- int endgreenVar;
- int endblueVar;
- int directionVar;
-
- public int startred() { return startredVar;}
- public void setstartred(int val) { startredVar = val; }
-
- public int startgreen() { return startgreenVar;}
- public void setstartgreen(int val) { startgreenVar = val; }
-
- public int startblue() { return startblueVar;}
- public void setstartblue(int val) { startblueVar = val; }
-
- public int endred() { return endredVar;}
- public void setendred(int val) { endredVar = val; }
-
- public int endgreen() { return endgreenVar;}
- public void setendgreen(int val) { endgreenVar = val; }
-
- public int endblue() { return endblueVar;}
- public void setendblue(int val) { endblueVar = val; }
-
- public int direction() { return directionVar;}
- public void setdirection(int val) {
- if ((val < 0) || (val > 3))
- directionVar = 0;
- else
- directionVar = val; }
-
-
- //CONSTRUCTORS
- public gradient() {
- this(0,0,0,255,255,255,0);
- }
-
- public gradient(int sred, int sgreen, int sblue,
- int ered, int egreen, int eblue,
- int dir) {
- this.setstartred(sred);
- this.setstartgreen(sgreen);
- this.setstartblue(sblue);
- this.setendred(ered);
- this.setendgreen(egreen);
- this.setendblue(eblue);
- this.setdirection(dir);
- }
-
- //THE RENDER METHOD
- void render(Graphics g, actor act, Region reg) {
- Color currentColor;
- Rectangle drawrect = reg.getBBox();
- Rectangle bounds = act.boundsrect();
- float steps = 1;
- switch (directionVar) {
- case 0: steps = bounds.width;
- break;
- case 1: steps = bounds.height;
- break;
- case 2: if (bounds.width > bounds.height)
- steps = bounds.width;
- else
- steps = bounds.height;
- break;
- case 3: if (bounds.width > bounds.height)
- steps = bounds.width;
- else
- steps = bounds.height;
- break;
- }
- float curRed = startredVar;
- float curGreen = startgreenVar;
- float curBlue = startblueVar;
-
- float stepRed = (endredVar - startredVar) / steps;
- float stepGreen = (endgreenVar - startgreenVar) / steps;
- float stepBlue = (endblueVar - startblueVar) / steps;
- int x1,x2,y1,y2;
- int i;
- switch (directionVar) {
- case 0:
- y1 = bounds.y;
- y2 = bounds.height;
- x1 = bounds.x;
- for (i = 0; i < steps; i++) {
- currentColor = new Color (Math.round(curRed),
- Math.round(curGreen),
- Math.round(curBlue));
- g.setColor(currentColor);
- g.fillRect(x1,y1,1,y2);
- curRed += stepRed;
- curGreen += stepGreen;
- curBlue += stepBlue;
- x1++; }
- break;
-
- case 1:
- x1 = bounds.x;
- x2 = bounds.width;
- y1 = bounds.y;
- for (i = 0; i < steps; i++) {
- currentColor = new Color (Math.round(curRed),
- Math.round(curGreen),
- Math.round(curBlue));
- g.setColor(currentColor);
- g.fillRect(x1,y1,x2,1);
- curRed += stepRed;
- curGreen += stepGreen;
- curBlue += stepBlue;
- y1++;}
- break;
- case 2:
- x1 = bounds.x;
- y1 = bounds.y;
- x2 = bounds.width;
- y2 = bounds.height;
- g.setColor(new Color (Math.round(curRed),
- Math.round(curGreen),
- Math.round(curBlue)));
- g.fillRect(x1,y1,drawrect.width,drawrect.height);
- for (i = 0; i < steps; i++) {
- g.setColor(new Color (Math.round(curRed),
- Math.round(curGreen),
- Math.round(curBlue)));
- g.fillOval(x1 + i,y1 + i,
- (x2 - i) - i, (y2 - i) - i);
- curRed += stepRed;
- curGreen += stepGreen;
- curBlue += stepBlue;}
- break;
- case 3:
- x1 = bounds.x;
- y1 = bounds.y;
- x2 = bounds.width;
- y2 = bounds.height;
- g.setColor(new Color (Math.round(curRed),
- Math.round(curGreen),
- Math.round(curBlue)));
- g.fillRect(x1,y1,drawrect.width,drawrect.height);
- for (i = 0; i < steps; i++) {
- g.setColor(new Color (Math.round(curRed),
- Math.round(curGreen),
- Math.round(curBlue)));
- g.fillRect(x1 + i,y1 + i,
- (x2 - i) - i, (y2 - i) - i);
- curRed += stepRed;
- curGreen += stepGreen;
- curBlue += stepBlue;}
- break;
- }
- }
-
-
- }
-